package com.example.yuejian21.nongfusanquan.yuejian21.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import com.example.yuejian21.nongfusanquan.yuejian21.fragment.ClubFragment;
import com.example.yuejian21.nongfusanquan.yuejian21.fragment.TabFragment;
import com.example.yuejian21.nongfusanquan.yuejian21.fragment.CourseFragment;
import com.example.yuejian21.nongfusanquan.yuejian21.fragment.MeFragment;
import com.example.yuejian21.nongfusanquan.yuejian21.fragment.PostFragment;
import com.example.yuejian21.nongfusanquan.yuejian21.R;


public class MainActivity extends FragmentActivity implements View.OnClickListener
{
    private LinearLayout mTabHomePage;
    private LinearLayout mTabClub;
    private LinearLayout mTabShow;
    private LinearLayout mTabPerson;
    private LinearLayout mTabFrd;

    private ImageButton mImgHomePage;
    private ImageButton mImgClub;
    private ImageButton mImgShow;
    private ImageButton mImgPerson;
    private ImageButton mImgFrd;

    private Fragment mTab01;
    private Fragment mTab02;
    private Fragment mTab03;
    private Fragment mTab04;
    private Fragment mTab05;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        initView();
        initEvent();//初始化事件
        setSelect(0);//默认选择第一个fragment
    }

    private void initEvent()
    {
        mTabHomePage.setOnClickListener(this);
        mTabClub.setOnClickListener(this);
        mTabShow.setOnClickListener(this);
        mTabFrd.setOnClickListener(this);
        mTabPerson.setOnClickListener(this);
    }

    private void initView()
    {
        mTabHomePage = (LinearLayout) findViewById(R.id.id_tab_shouye);
        mTabClub = (LinearLayout) findViewById(R.id.id_tab_club);
        mTabShow = (LinearLayout) findViewById(R.id.id_tab_show);
        mTabFrd = (LinearLayout) findViewById(R.id.id_tab_frd);
        mTabPerson = (LinearLayout) findViewById(R.id.id_tab_person);

        mImgHomePage = (ImageButton) findViewById(R.id.shouye);
        mImgClub = (ImageButton) findViewById(R.id.club);
        mImgShow = (ImageButton) findViewById(R.id.show);
        mImgPerson = (ImageButton) findViewById(R.id.person);
        mImgFrd = (ImageButton) findViewById(R.id.id_tab_frd_img);
    }

    private void setSelect(int i)
    {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);

        switch (i)
        {
            case 0:
                if (mTab01 == null)
                {
                    mTab01 = new CourseFragment();
                    transaction.add(R.id.id_content, mTab01);
                } else
                {
                    transaction.show(mTab01);
                }
                mImgHomePage.setImageResource(R.drawable.course_press);
                break;
            case 1:
                if (mTab02 == null)
                {
                    mTab02 = new ClubFragment();transaction.add(R.id.id_content, mTab02);
                } else
                {
                    transaction.show(mTab02);

                }
                mImgClub.setImageResource(R.drawable.club_press);
                break;
            case 2:
                if (mTab03 == null)
                {
                    mTab03 = new TabFragment();transaction.add(R.id.id_content, mTab03);
                } else
                {
                    transaction.show(mTab03);

                }
                mImgFrd.setImageResource(R.drawable.activity_press);
                break;
            case 3:
                if (mTab04 == null)
                {
                    mTab04 = new PostFragment();
                    transaction.add(R.id.id_content, mTab04);
                } else
                {
                    transaction.show(mTab04);
                }
                mImgShow.setImageResource(R.drawable.neighborhood_press);
                break;
            case 4:
                if (mTab05 == null)
                {
                    mTab05 = new MeFragment();
                    transaction.add(R.id.id_content, mTab05);
                } else
                {
                    transaction.show(mTab05);
                }
                mImgPerson.setImageResource(R.drawable.me_press);
                break;

            default:
                break;
        }

        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction)
    {
        if (mTab01 != null)
        {
            transaction.hide(mTab01);
        }
        if (mTab02 != null)
        {
            transaction.hide(mTab02);
        }
        if (mTab03 != null)
        {
            transaction.hide(mTab03);
        }
        if (mTab04 != null)
        {
            transaction.hide(mTab04);
        }
        if (mTab05 != null)
        {
            transaction.hide(mTab05);
        }
    }

    @Override
    public void onClick(View v)
    {
        resetImgs();
        switch (v.getId())
        {
            case R.id.id_tab_shouye:
                setSelect(0);
                break;
            case R.id.id_tab_club:
                setSelect(1);
                break;
            case R.id.id_tab_frd:
                setSelect(2);
                break;
            case R.id.id_tab_show:
                setSelect(3);
                break;
            case R.id.id_tab_person:
                setSelect(4);
                break;

            default:
                break;
        }
    }

    /**
     * �л�ͼƬ����ɫ
     */
    private void resetImgs()
    {
        mImgHomePage.setImageResource(R.drawable.course);
        mImgClub.setImageResource(R.drawable.club);
        mImgShow.setImageResource(R.drawable.neighborhood);
        mImgPerson.setImageResource(R.drawable.me);
        mImgFrd.setImageResource(R.drawable.activity);
    }

}
